package com.zdx.yswl.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.zdx.yswl.App;
import com.zdx.yswl.R;
import com.zdx.yswl.base.KException;
import com.zdx.yswl.http.KAsyncTask.TaskDelegate;
import com.zdx.yswl.tool.KSystemTool;
import com.zdx.yswl.ui.adapter.ConsultantAdapter;
import com.zdx.yswl.ui.base.BaseActivity;
import com.zdx.yswl.ui.bean.ConsultantBean;
import com.zdx.yswl.ui.exception.AppException;
import com.zdx.yswl.ui.exception.NoDataException;
import com.zdx.yswl.ui.task.CallConsultantCountTask;
import com.zdx.yswl.ui.task.ConsultantTask;
import com.zdx.yswl.ui.task.PraiseTask;
import com.zdx.yswl.view.dialog.ConfirmDialog;
import com.zdx.yswl.view.dialog.ConfirmDialog.ConfirmDialogDelegate;

public class ConsultantActivity extends BaseActivity implements TaskDelegate {

	protected static final String TAG = ConsultantActivity.class.getSimpleName();

	TextView msg;
	ConsultantAdapter adapter;
	ListView listView;

	private String requestId;

	public static Intent actionStart(Context context, String requestId) {
		Intent intent = new Intent(context, ConsultantActivity.class);
		intent.putExtra("projectId", requestId);
		return intent;
	}

	@Override
	protected void initSettings()
	{

	}

	@Override
	protected void initUi()
	{

	}

	@Override
	protected void saveSettings()
	{

	}

	@Override
	protected void freeMe()
	{

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_consultant_list);

		requestId = getIntent().getStringExtra("projectId");
		setHeader("置业顾问");
		initData();
	}

	private void initData() {
		listView = (ListView) findViewById(R.id.property_consultant);
		adapter = new ConsultantAdapter(this, new OnClickListener() {
			@Override
			public void onClick(View v) {
				int position = (Integer) v.getTag();
				String consultantId = adapter.dataList.get(position).id;
				if (v.getId() == R.id.contacts_zan_btn) {
					int count = adapter.dataList.get(position).goodCount;
					long currentTime = System.currentTimeMillis();
					long praise = App.getInstance().getPraiseTime(ConsultantActivity.this,consultantId);//点赞时间
					if (praise != 0) {
						long  tempTime = (currentTime - praise) / (60*60*1000);
						int tiem= new Long(tempTime).intValue();
						if (tiem < 3) {
							Toast.makeText(ConsultantActivity.this, "3小时内不能重复点赞", Toast.LENGTH_SHORT).show();
							return;
						}
						
					}
					
					int ii = count + 1;
					((Button) v).setText(ii+"");
					((Button) v).setBackgroundResource(R.drawable.goodbutton_selector_two);
					//记录时间
					
					App.getInstance().recordCurrentTime(ConsultantActivity.this, System.currentTimeMillis(),consultantId);
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("agentId", consultantId);
					PraiseTask task = new PraiseTask( 20010, ConsultantActivity.this);
					task.execute(map);
				}else if(v.getId() == R.id.contacts_call_btn){
					startActivity(KSystemTool.call_2(adapter.dataList.get(position).tel));
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("agentId", consultantId);
					CallConsultantCountTask task = new CallConsultantCountTask( 20012, ConsultantActivity.this);
					task.execute(map);
				}
				
			}

		});
		listView.setAdapter(adapter);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectId", requestId);
		ConsultantTask task = new ConsultantTask(20022, this);
		task.execute(map);

//		listView.setVisibility(View.GONE);
		startProgress();
	}

	/** 友盟session的统计 */
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	/** 友盟session的统计 */
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
		closeProgress();
		if (object instanceof NoDataException) {
			Toast.makeText(this, ((NoDataException) object).getMessage(), Toast.LENGTH_SHORT).show();;
		} else if (object instanceof KException) {
//			ConfirmDialog.share(this, -1, new ConfirmDialogDelegate() {
//				@Override
//				public void onClick_ConfirmDialog(int __id, int __position) {
//					finish();
//				}
//			});
//			ConfirmDialog.showDialog("提示", ((KException) object).getMessage(), "确定");
		}
	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		closeProgress();
		if (taskId == 20010) {

		} else if (taskId == 20012) {
//			startActivity(KSystemTool.call_2(adapter.dataList.get(position).tel));
		} else if (taskId == 20022) {
			listView.setVisibility(View.VISIBLE);
			adapter.dataList = (ArrayList<ConsultantBean>) object;
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// backActivity();
			finish();
		}
		return true;
	}

	@Override
	public void onNotify(int what, int arg1, int arg2, Object obj)
	{

	}

	@Override
	public void onHandler(int what, int arg1, int arg2, Object obj)
	{

	}
}
